<template>
  <div class="home flex-col space-between align-center w-100 h-100 pr">
    <div class="signin-form pd-2">
      <svg-icon name="avatar" :size="50" class="avatar"></svg-icon>
      <el-form :model="signinForm" status-icon :rules="rules.USER_RULES" ref="signinForm" label-width="0px" class="demo-signinForm">
        <el-form-item prop="username">
          <el-input  v-model="signinForm.username" autocomplete="off" placeholder="你的账号">
            <svg-icon slot="suffix" name="account" class="suffix-icon"></svg-icon>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="signinForm.password" autocomplete="off" placeholder="你的密码">
            <svg-icon slot="suffix" name="password" class="suffix-icon"></svg-icon>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="signin">登录</el-button>
          <el-button @click="signout">退出登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { USER } from '../../global/constant'
import { LoginApi } from '../../global/api'
export default {
  name: "home",
  data() {
    return {
      signinForm: {
        username: '',
        password: ''
      },
      rules: USER,
      loginApi: new LoginApi()
    }
  },
  methods: {
    async signin() {
      const res = await this.loginApi.signin(this.signinForm)
      if(res.code === 200) {
        this.$router.push('/user')
      }
    },
    async signout() {
      const res = await this.loginApi.signout()
      console.log(res);
    }
  },
  async beforeRouteLeave(to, from, next) {
    const res = await this.loginApi.isAuth()
    if(res.data) {
      next()
    }
  }
}
</script>

<style lang="less" scoped>
.home {
  background-image: url(../../assets/signin-bg.svg);
  background-size: cover;
  background-position: center;
  .signin-form {
    margin: 8% auto;
    width: 300px;
    background-color: #fff;
    box-shadow: 0 0 3px 0 rgba(0, 0, 0, .3);
    text-align: center;

    .avatar {
      margin-top: -66px;
    }

    ::v-deep .el-input__inner:focus + .el-input__suffix {
      .suffix-icon {
        color: rgba(0, 0, 0, .8);
      }
    }

    .suffix-icon {
      color: rgba(0, 0, 0, .3);
      transition: color 1s linear;
      &:hover {
        color: rgba(0, 0, 0, .8);
      }
    }
  }
}

::v-deep .el-input__suffix {
  right: 16px;
}
</style>